/*	
 * xiao  
 * 2018/9/29 
 * 1.个人注册发送验证码     未完成      验证码：8888   83525102 邀请码
 * 2.个人注册下一步  
 * 3.企业注册下一步
 */

mui.init();
var mask = mui.createMask(); //遮罩层
var city_leaber = "86";
var cityPicker3 = null;
var phone_type = "domestic";



//地区
/*三级联动*/
(function($, doc) {
	$.init();
	$.ready(function() {
		/**
		 * 获取对象属性的值
		 * 主要用于过滤三级联动中，可能出现的最低级的数据不存在的情况，实际开发中需要注意这一点；
		 * @param {Object} obj 对象
		 * @param {String} param 属性名
		 */
		var _getParam = function(obj, param) {
			return obj[param] || '';
		};
		if(city_leaber == "86" || city_leaber == "00886") {
			cityPicker3 = new $.PopPicker({
				layer: 3
			});
			cityPicker3.setData(cityData3);
			var showCityPickerButton = doc.getElementById('region');
			var cityResult3 = doc.getElementById('region');
			showCityPickerButton.addEventListener('tap', function(event) {
				cityPicker3.show(function(items) {
					cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
					//返回 false 可以阻止选择框的关闭
					//return false;
				});
			}, false);
		}
	});
})(mui, document);

//注册发送验证码
$(document).on('tap', '#btn', function() {
	var phone = $("#quick_register_phone2").val(); //获取注册手机号
	var mobile = $("#quick_register_phone2").val(); //获取注册手机号
	var region_text_code = $("#region_text_code").text(); //地区区号
//	var phone_code = region_text_code.split("+")[1]; //截取
//	var phone_sure_code = phone_code.split(")")[0]; //截取
//	if(phone_sure_code == "86") {
//		phone = $("#quick_register_phone2").val(); //获取注册手机号
//	} else {
//		phone = phone_sure_code + phone
//	}
	//var phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/; //手机号验证
	if(!phone) {
		mui.alert("手机号码不能为空");
		return false;
	}
	//	if(phoneReg.test(phone)) {
	//		verificationCode(phone);
	//	} else {
	//		mui.alert("请填写有效的手机号");
	//		$("#quick_register_phone2").val("");
	//		return false;
	//	}
	verificationCode(phone,mobile);
})

function verificationCode(phone,mobile) { //企业验证码接口
	
//	var dataAddCart = {
//		"phone": phone,
//		"sign": "string",
//		"smsChannel": 'domestic',
//		"type": "REGISTER_VERIFICATION_CODE"
//	};
//	$.ajax({
//		url:user_url+'/api/register/verificationCode',
//		type:"post",
//		dataType:'json',
//		data:dataAddCart,
//		success:function (datas) {
//			console.log('注册55');
//			console.log(JSON.stringify(datas));
//		},         
//		error: function(res) {
//			console.log('失败66');					
//		},
//	})
	
	mui.ajax(user_url + '/api/register/verificationCode', {
		data: {
			"phone": phone,
			"sign": "string",
			"smsChannel": 'domestic',
			"type": "REGISTER_VERIFICATION_CODE"
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'POST', //HTTP请求类型
		timeout: 50000, //超时时间设置为10秒；
		success: function(data) {
			console.log(JSON.stringify(data))
			if(data.code == 0) {
				function invokeSettime(obj) {
					var countdown = 60;
					settime(obj);
					function settime(obj) {
						if(countdown == 0) {
							$(obj).attr("disabled", false);
							$(obj).text("获取验证码");
							countdown = 60;
							return;
						} else {
							$(obj).attr("disabled", true);
							$(obj).text("重新发送(" + countdown + "s)");
							countdown--;
						}
						setTimeout(function() {
							settime(obj)
						}, 1000)
					}
				}
				new invokeSettime("#btn");
			}
			else{
				mui.alert(data.msg)
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(type);
			var error_msg = JSON.parse(xhr.responseText)
			mui.alert(error_msg.msg)
		}
	});
}

//同意协议
mui('body').on('tap', '.readbox em font', function() {
	$('.register-article').show();
});
mui('body').on('tap', '.register-article .sure', function() {
	$('.register-article').hide();	
	$('input[type="checkbox"]#read').prop('checked', true);
});
mui('body').on('tap', '.register-article .cancel', function() {
	$('.register-article').hide();
});

//个人注册下一步
$(document).on("tap", "#enterprise", function() {
	console.log('注册')
	var sta = $("#item_s").attr("sta"); //注册类型
	var phone = $("#quick_register_phone2").val(); //获取手机号码
	var mobile = $("#quick_register_phone2").val(); //获取手机号码
	var pwVal = $('#quick_register_pw').val();
//	console.log(mobile)
	var region_text_code = $("#region_text_code").text(); //地区区号
//	var phone_code = region_text_code.split("+")[1]; //截取
//	var phone_sure_code = phone_code.split(")")[0]; //截取
	
	var phone_verification_code = $("#quick_register_code2").val(); //获取手机验证码
	var Invitation_code = $("#Invitation_code2").val() //邀请码
	var region = $("#region").text(); //地区
	if(!phone) {
		mui.alert("请输入手机号码");
		return false;
	}
	if(Invitation_code){
		if(!(/^1[34578]\d{9}$/.test(Invitation_code))){
   		 	mui.alert("请输入邀请的手机号码");
			return false
		}
	}
	if(pwVal=='') {
		mui.alert("请输入登录密码");
		return false;
	}
	var serviceCheck = $('input[type="checkbox"]#read').prop('checked');
	if(serviceCheck==false){
		mui.alert("请阅读用户注册《惠民服务协议》，并同意！");
		return false;
	}
//	if(Invitation_code=='') {
//		mui.alert("请输入邀请手机号码");
//		return false;
//	}
	plus.nativeUI.showWaiting("加载中...");
	mask.show(); //显示遮罩层
	var regionStr = '';
	regionStr = region;
	if(region=='地区'){
		regionStr = '';
	}
	
	var cc = {
			'password' : pwVal,
			'phone': phone,
			'verification_code': phone_verification_code,
			'referral_code': Invitation_code,
			'user_id': 0,
			'sign': 'string',
			'region':regionStr
		}
	console.log(JSON.stringify(cc))
	mui.ajax(user_url + '/api/register/register', {
		data: cc,
		dataType: 'json', //服务器返回json格式数据 
		type: 'post', //HTTP请求类型
		timeout: 50000, //超时时间设置为10秒；
		success: function(data) {
			console.log(JSON.stringify(data))
			plus.nativeUI.closeWaiting(); //关闭等待
			mask.close(); //关闭遮罩层
			if(data.code==0){
				mui.openWindow({
					url: 'login.html',
					createNew: true
				})
				return
			}
			else{
				mui.alert(data.msg);
				return false;
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log('接口失败11')
			plus.nativeUI.closeWaiting(); //关闭等待
			mask.close(); //关闭遮罩层
		}
	});

})
//点击跟换地区
$(document).on('tap', '.region_text', function() {
	var region_text = $(this).text(); //地区值
	var region_text_Country = region_text.split("(")[0];
	$("#region_text_Country").text(region_text_Country);
	var region_text_code = region_text.split("(")[1];
	$("#region_text_code").text("(" + region_text_code);
	var city_leabers = $(this).children("a").attr('leaber_num');
	if(city_leabers == "86") {
		phone_type = "domestic";
	} else {
		phone_type = "international";
	}
	/*三级联动*/
	(function($, doc) {
		$.init();
		$.ready(function() {
			/**
			 * 获取对象属性的值
			 * 主要用于过滤三级联动中，可能出现的最低级的数据不存在的情况，实际开发中需要注意这一点；
			 * @param {Object} obj 对象
			 * @param {String} param 属性名
			 */
			var _getParam = function(obj, param) {
				return obj[param] || '';
			};

			if(city_leabers == "86") {
				cityPicker3 = new $.PopPicker({
					layer: 3
				});
				cityPicker3.setData(cityData3);
				var showCityPickerButton = doc.getElementById('region');
				var cityResult3 = doc.getElementById('region');
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);
			} else if(city_leabers == "00886") {
				cityPicker3 = new $.PopPicker({
					layer: 3
				});
				cityPicker3.setData(cityData5);
				var showCityPickerButton = doc.getElementById('region');
				var cityResult3 = doc.getElementById('region');
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);
			} else if(city_leabers == "60") {
				cityPicker3 = new $.PopPicker({
					layer: 3
				});
				cityPicker3.setData(cityData4);
				var showCityPickerButton = doc.getElementById('region');
				var cityResult3 = doc.getElementById('region');
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);
			} else if(city_leabers == "00852") {

				cityPicker3 = new $.PopPicker({
					layer: 3
				});
				cityPicker3.setData(cityData6);
				var showCityPickerButton = doc.getElementById('region');
				var cityResult3 = doc.getElementById('region');
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);

			} else {

				cityPicker3 = new $.PopPicker({
					layer: 3
				});
				cityPicker3.setData(cityData7);
				var showCityPickerButton = doc.getElementById('region');
				var cityResult3 = doc.getElementById('region');
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);

			}

		});
	})(mui, document);
})

$(document).on('tap', '.return', function() {
	window.top.location.href = "login.html"
})


//页面进入执行方法
window.onload = function() {
	mui.plusReady(function() {
		var self = plus.webview.currentWebview();
		uid = self.uid;
		if(uid!=undefined){
			console.log('二维码带回注册的推荐人电话号码');
			console.log(uid);
			plus.nativeUI.showWaiting("加载中...");
			mask.show(); //显示遮罩层
			mui.ajax(user_url + '/api/user/getOneTel', {
				data: {
					id:uid
				},
				dataType: 'json', //服务器返回json格式数据 
				type: 'post', //HTTP请求类型
				timeout: 50000, //超时时间设置为10秒；
				success: function(data) {
					console.log(JSON.stringify(data))
					plus.nativeUI.closeWaiting(); //关闭等待
					mask.close(); //关闭遮罩层
					if(data.code==10000){
						$('#Invitation_code2').val(data.resp);
					}
					else{
						mui.alert(data.msg);
						return false;
					}
				},
				error: function(xhr, type, errorThrown) {
					//异常处理；
					console.log('接口失败11')
					plus.nativeUI.closeWaiting(); //关闭等待
					mask.close(); //关闭遮罩层
				}
			});
		}		
		plus.nativeUI.closeWaiting(); //关闭等待
		mask.close(); //关闭遮罩层
		//页面刷新
		window.addEventListener('refresh_method', function(e) { //执行刷新
			location.reload();
		});
	});
}